package com.zego.wawaji.third.pay.util;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.noble.library.basic.encrypt.EncryptUtil;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.zego.wawaji.base.toast.PToast;
import com.zego.wawaji.third.ThirdConstant;
import com.zego.wawaji.third.pay.bean.BasicNameValuePair;
import com.zego.wawaji.third.pay.bean.NameValuePair;
import com.zego.wawaji.third.pay.bean.PayWX;

import java.util.LinkedList;
import java.util.List;

/**
 * 微信支付SDK调用
 */
public class PayWeixinUtils {
    private Context context;
    private IWXAPI api;

    public PayWeixinUtils(Context context) {
        this.context = context;
        api = WXAPIFactory.createWXAPI(context, "");
    }

    /**
     * 关闭支付等待的透明窗体
     */
    public void closePayOrderAct() {
        if (!(context instanceof Activity)) return;
        Activity act = (Activity) context;
        View view = ((ViewGroup) act.findViewById(android.R.id.content)).getChildAt(0);
        if (view == null)
            act.finish();
    }

    /**
     * 调用SDK支付
     */
    public void pay(PayWX payWX) {
        try {
            ThirdConstant.WEIXIN_APP_ID = payWX.getApp_id();

            PayReq req = new PayReq();
            req.appId = payWX.getApp_id();
            req.partnerId = payWX.getMch_id();
            req.prepayId = payWX.getPrepay_id();
            req.nonceStr = payWX.getNonce_str();
            req.timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
            req.packageValue = "Sign=WXPay";

            List<NameValuePair> signParams = new LinkedList<>();
            signParams.add(new BasicNameValuePair("appid", req.appId));
            signParams.add(new BasicNameValuePair("noncestr", req.nonceStr));
            signParams.add(new BasicNameValuePair("package", req.packageValue));
            signParams.add(new BasicNameValuePair("partnerid", req.partnerId));
            signParams.add(new BasicNameValuePair("prepayid", req.prepayId));
            signParams.add(new BasicNameValuePair("timestamp", req.timeStamp));
//
            req.sign = genAppSign(signParams);

            api.registerApp(req.appId);
            api.sendReq(req);
        } catch (Exception e) {
            e.printStackTrace();
            PToast.showShort("订单数据出错，请重新请求!");
            closePayOrderAct();
        }
    }

    private static String genAppSign(List<NameValuePair> params) {
        StringBuilder ss = new StringBuilder();

        for (int i = 0; i < params.size(); i++) {
            ss.append(params.get(i).getName());
            ss.append('=');
            ss.append(params.get(i).getValue());
            ss.append('&');
        }
        ss.append("key=");

        ss.append(ThirdConstant.WEIXIN_App_Key);
        String appSign = EncryptUtil.md5(ss.toString()).toUpperCase();
        Log.e("orion", appSign);
        return appSign;
    }
}
